local redisKey = KEYS[1];
local maxCount = tonumber(ARGV[1]);
local expireTimeSeconds = tonumber(ARGV[2]);

if redis.call('exists', redisKey) == 1 then
    local count = tonumber(redis.call('get', redisKey))
    if count < maxCount then
        redis.call('incr', redisKey)
        return count + 1
    else
        redis.call('set', redisKey, 1)
        redis.call('expire', redisKey, expireTimeSeconds)
        return 1
    end
else
    redis.call('set', redisKey, 1)
    redis.call('expire', redisKey, expireTimeSeconds)

    return 1
end
